<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        let K = null;
        let N = null;
        let word_lis = [];
        let res = '';

        let readline = `0
9
word
dd
da
ab
bd
cd
dc
dword
d`
        readline = readline.split('\n').map((v) => { return v.trim() })
        K = readline[0]
        N = readline[1]
        readline = readline.slice(2)
        let str_first = readline.splice(K, 1)
        // console.log(readline);

        //以单词开头第一个字母分组
        let str_lis = Array.from(new Array(26), () => [false])
        readline.forEach((v, i) => {
            str_lis[v[0].charCodeAt() - 'a'.charCodeAt()][0] = true
            str_lis[v[0].charCodeAt() - 'a'.charCodeAt()].push(v)
        });

        //每组进行排序        
        str_lis.forEach((v, i) => {
            v.sort((a, b) => {
                if (a.length != b.length) {
                    return b.length - a.length
                } else {
                    return a.localeCompare(b)
                }
                return v
            })
        })

        //接龙
        let [str] = str_first;//第一个单词接龙过来
        let tag = true //标记能不能接龙

        while (tag) {
            let last_str = str[str.length - 1]
            let j = last_str.charCodeAt() - 'a'.charCodeAt()
            if (str_lis[j][0]) {
                str += str_lis[j][1]
                str_lis[j].splice(1,1)
                if (str_lis[j].length === 1) {
                    str_lis[j][0] = false
                }
            } else {
                tag = false
            }
        }
        console.log(str);
















    </script>
</body>

</html>